package com.example.assignment01_javaee.Controller;

import com.example.assignment01_javaee.Entity.Customer;
import com.example.assignment01_javaee.Service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @GetMapping("/customers")
    public ResponseEntity<List<Customer>> getAllCustomers() {
        List<Customer> customers = customerService.getAllCustomers();
        return ResponseEntity.ok(customers);
    }

    @GetMapping("/customers/{customerId}")
    public ResponseEntity<Customer> getCustomerById(@PathVariable int customerId) {
        Customer customer = customerService.getCustomerById(customerId);
        return ResponseEntity.ok(customer);
    }

    @PostMapping("/customers")
    public String createCustomer(@RequestBody Customer customer) {
        customerService.createCustomer(customer);
        return "增加顾客信息成功";
    }

    @PutMapping("/customers/{customerId}")
    public String updateCustomer(@PathVariable int customerId, @RequestBody Customer customer) {
        customer.setCustomerId(customerId);
        customerService.updateCustomer(customer);
        return "修改顾客信息成功";
    }

    @DeleteMapping("/customers/{customerId}")
    public String deleteCustomer(@PathVariable int customerId) {
        customerService.deleteCustomer(customerId);
        return "删除顾客信息成功";
    }
}
